package com.eqdd.yiqidian.ui.companyspace

import android.support.v7.widget.RecyclerView
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.bean.CompanyInfoBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimImageSelectBean
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.kotlin.imgPathPreview
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_recyclerview.*
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-6-30.
 */
class CompanyInfoActivity : SlimBaseMultiEditActivity() {

    private var companyInfoBean: CompanyInfoBean? = null

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview)
        initTopTitleBar(View.VISIBLE, "企业信息")
    }

    override fun initRecyclerView(): RecyclerView {
        return recyclerView
    }

    override fun itemClick(p: Int) {

    }

    override fun initData() {
        super.initData()
        OkGo.post<HttpResult<CompanyInfoBean>>(HttpConfig.BASE_URL + HttpConfig.COMPANY_INFO)
                .params("companyId", user.companyId)
                .execute(object : DialogCallBack<HttpResult<CompanyInfoBean>>(this) {
                    override fun onSuccess(response: Response<HttpResult<CompanyInfoBean>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            companyInfoBean = result.items;
                            setView()
                        } else {
                            toast(result?.msg ?: "获取信息失败")
                            finish()
                        }
                    }
                })
    }

    override fun setEditBeans() {
        datas?.clear()
        val stringBuilder = StringBuilder()
        companyInfoBean?.placelist?.forEach {
            stringBuilder.append(it.place).append("\n")
        }

        val location = if (stringBuilder.length > 1) {
            stringBuilder.substring(0, stringBuilder.length - 2)
        } else {
            ""
        }

        datas?.add(SlimEditHintBean("品牌名称", companyInfoBean?.brand, 0))
        datas?.add(SlimEditHintBean("成立时间", companyInfoBean?.establishTime, 1))
        datas?.add(SlimEditHintBean("客户群体", companyInfoBean?.customerGroup, 2))
        datas?.add(SlimEditHintBean("经营范围", companyInfoBean?.scopeOfOperation, 3))
        datas?.add(SlimEditHintBean("注册资本", companyInfoBean?.registeredCapital, 4))
        datas?.add(SlimEditHintBean("登记机关", companyInfoBean?.register, 5))
        datas?.add(SlimEditHintBean("经营行业", companyInfoBean?.industry, 6))
        datas?.add(SlimEditHintBean("经营模式", companyInfoBean?.management, 7))
        datas?.add(SlimEditHintBean("经营期限", companyInfoBean?.businessTime, 8))
        datas?.add(SlimEditHintBean("年营业额", companyInfoBean?.turnover, 9))
        datas?.add(SlimEditHintBean("体系认证", companyInfoBean?.authentication, 10))
        datas?.add(SlimEditHintBean("产品商标", companyInfoBean?.trademark, 11))
        datas?.add(SlimImageSelectBean("商标图片", 12, companyInfoBean?.trademarkImages))
        datas?.add(SlimEditHintBean("厂房面积", companyInfoBean?.workshopArea, 13))
        datas?.add(SlimEditHintBean("主营地址", location, 14))
        slimAdapterEx?.register(R.layout.layout_image_selected, SlimInjector<SlimImageSelectBean> { data, injector ->
            injector?.text(R.id.tv_label, data?.title)
                    ?.image(R.id.iv_add, data?.image)
                    ?.clicked(R.id.iv_add) {
                        data?.image?.let { it1 -> imgPathPreview(it1) }
                    }
        })

    }


}